package com.example.poi.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.tomcat.util.http.fileupload.util.Streams;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author 作者 zhujf:
 * @version 创建时间：2017年9月11日 下午7:50:39
 * 导入excel插入数据库
 */
@Controller
public class UploadController {

	@RequestMapping(value = "uploadfileexcel", method = RequestMethod.POST)
	@ResponseBody
	public void upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception {

		// 分别获取的是变量名file---文件类型---文件名
		System.out.println(file.getName() + "---" + file.getContentType() + "---" + file.getOriginalFilename());
		int last = file.getOriginalFilename().lastIndexOf(".");
		System.out.println(last);
		String filename = file.getOriginalFilename();
		System.out.println(filename);
		// 判断是否为excel类型文件
		if (!filename.endsWith(".xls") || !filename.endsWith(".xlsx")) {
			System.out.println("文件不是excel类型");
		}
		// 获取项目在服务器上的路径
		String path = request.getSession().getServletContext().getRealPath("");

		// 第二种方式，获取文件流
		FileInputStream fis = null;
		try {
			fis = (FileInputStream) file.getInputStream();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 这是第二种方式，直接把文件流传过去
		String mark = xls2String(fis, request);
		if ("sucs".equals(mark)) {

		} else if ("err".equals(mark)) {

		}

		System.out.println(mark);
	}

	public String xls2String(FileInputStream fis, HttpServletRequest request) {

		Workbook hssfWorkbook = null;
		// 2003版本的excel，用.xls结尾
		try {
			hssfWorkbook = new HSSFWorkbook(fis);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // 得到工作簿

		// 循环工作表Sheet
		for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
			HSSFSheet hssfSheet = (HSSFSheet) hssfWorkbook.getSheetAt(numSheet);
			if (hssfSheet == null) {
				continue;
			}
			// 循环行Row
			for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
				HSSFRow hssfRow = hssfSheet.getRow(rowNum);
				if (hssfRow != null) {
					double a = hssfRow.getCell(0).getNumericCellValue();
					double b = hssfRow.getCell(1).getNumericCellValue();
					boolean c = hssfRow.getCell(2).getBooleanCellValue();
					Date d = hssfRow.getCell(3).getDateCellValue();
					HSSFCell e = hssfRow.getCell(4);
					HSSFCell f = hssfRow.getCell(5);
					HSSFCell g = hssfRow.getCell(6);
					HSSFCell h = hssfRow.getCell(7);
					
				}
			}
		}

		return "";
	}

	
}
